Quick start
[1]:
%load_ext autoreload
%autoreload 2
from py3Dinterpolations.core import GridData
from py3Dinterpolations.modelling.interpolation import Interpolator3D
from py3Dinterpolations.plotting.plotting import plot_3d_model
import pandas as pd
Import data from pandas
The package is written so to be able to import data from pandas. The data must be in a pandas dataframe.
Column names could be either default [“X”, “Y”, “Z”,”V”] or custom. In the latter case, the user must specify the column names in the function call.
[2]:
df = pd.read_csv(
"../../../tests/fixtures/griddata_default_colnames.csv",
)
df.tail()
[2]:
| ID | X | Y | Z | V | |
|---|---|---|---|---|---|
| 508 | ID00 | 15.194 | 0.0 | 8 | 25.080662 |
| 509 | ID00 | 15.194 | 0.0 | 7 | 19.047857 |
| 510 | ID00 | 15.194 | 0.0 | 6 | 19.042223 |
| 511 | ID00 | 15.194 | 0.0 | 5 | 14.111599 |
| 512 | ID00 | 15.194 | 0.0 | 4 | 11.667224 |
GridData object
This object is designed to store the data that is being interpolated in an efficient manner. It is a wrapper around a pandas dataframe, with capability to return the equivalent numpy array for interaction with interpolation packages (eg. pyKrige, scipy)
[3]:
gd = GridData(df)
gd.data
[3]:
| V | ||||
|---|---|---|---|---|
| ID | X | Y | Z | |
| ID30 | 62.163 | 14.336 | 19 | 7.523950 |
| 18 | 7.504403 | |||
| 17 | 10.089374 | |||
| 16 | 15.369461 | |||
| 15 | 11.836174 | |||
| ... | ... | ... | ... | ... |
| ID00 | 15.194 | 0.000 | 8 | 25.080662 |
| 7 | 19.047857 | |||
| 6 | 19.042223 | |||
| 5 | 14.111599 | |||
| 4 | 11.667224 |
513 rows × 1 columns
Interpolate
Interpolate by calling the Interpolator3D objects, that wraps up all ncessary functions to interpolate the data.
Calulcates required 3d grid for prediction
Executes preprocessing
Fit the model
Executes interpolation
[4]:
interpolation = Interpolator3D(
gd,
model_type="statistical",
model_name = "ordinary_kriging",
model_params = {
"variogram_model": "spherical",
"nlags": 15,
"weight": True,
"exact_values": False,
"verbose": True,
"enable_plotting": True,
},
grid_resolution=1,
preprocess_kwags={
"normalize_xyz": True,
"standardize_v": True,
}
)
Plotting Enabled
Adjusting data for anisotropy...
Initializing variogram model...
Using 'spherical' Variogram Model
Partial Sill: 0.6919671836967347
Full Sill: 1.180064665388263
Range: 0.9483118335673657
Nugget: 0.48809748169152833
Calculating statistics on variogram model fit...
Q1 = 0.06289183469394131
Q2 = 0.8427221552135199
cR = 0.5645159804206736
[5]:
interpolation.interpolate()
Executing Ordinary Kriging...
3D Visualization with Plotly
The package natively supports matplotlib and plotly for visualization.
[6]:
import plotly.io as pio
# This ensures Plotly output works in multiple places:
# plotly_mimetype: VS Code notebook UI
# notebook: "Jupyter: Export to HTML" command in VS Code
# See https://plotly.com/python/renderers/#multiple-renderers
pio.renderers.default = "plotly_mimetype+notebook"
[7]:
fig = plot_3d_model(
interpolation.model,
plot_points=True,
scale_points=10,
surface_count=20,
opacityscale=[(0,0),(1,1)]
)
fig.show()